package com.wrupple.muba.catalogs.client.activity.process.state.impl;

import com.google.gwt.event.shared.EventBus;
import com.wrupple.muba.catalogs.client.activity.process.state.ContentLoadingState;
import com.wrupple.muba.catalogs.domain.CatalogEntry;
import com.wrupple.muba.catalogs.domain.CatalogProcessDescriptor;
import com.wrupple.muba.common.client.application.DataCallback;
import com.wrupple.muba.common.shared.StateTransition;

public class ContentLoadingStateImpl implements ContentLoadingState {

	private StorageManager storage;
	
	class ModifyLoadedEntry extends DataCallback<CatalogEntry>{
		 StateTransition<CatalogEntry> onDone;
		 
		public ModifyLoadedEntry(StateTransition<CatalogEntry> onDone) {
			super();
			this.onDone = onDone;
		}

		@Override
		public void execute() {
			alterLoadedEntry(result);
		}
		
	}

	public ContentLoadingStateImpl() {
		super();
	}


	@Override
	public void start(CatalogProcessDescriptor parameter, final StateTransition<CatalogEntry> onDone,EventBus bus) {
		String entryId = parameter.getSelectedValueId();
		String catalog = parameter.getSelectedType();
		CatalogEntry regreso = null;
		if (entryId == null) {
			regreso = CatalogEntry.createCatalogEntry(catalog);
			alterLoadedEntry(regreso);
			onDone.setResult(regreso);
			onDone.execute();
		} else {
			storage.read(catalog,entryId, onDone);
		}
	}

	@Override
	public void setContext(com.wrupple.muba.common.shared.State.Context context) {
		this.storage = context.getStorageManager();
	}
	protected void alterLoadedEntry(CatalogEntry result) {
		
	}
}
